//
//  MessageVC.m
//  AirQuality
//
//  Created by Po on 15/9/11.
//  Copyright © 2015年 Po. All rights reserved.
//

#import "MessageVC.h"
#import "MessageModleHelper.h"
#import "MessageCell.h"
#import "UIImageView+WebCache.h"

#import "MessageImageShowVC.h"

@interface MessageVC () <UITableViewDataSource, UITableViewDelegate, MessageCellDelegate>

@property (retain, nonatomic) UITableView * talbeView;
@property (retain, nonatomic) RJLoadView * myLoadView;

@property (retain, nonatomic) MessageModleHelper * helper;
@property (retain, nonatomic) NSMutableArray * heightArray;

@end

@implementation MessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navBar setReturnButtonHidden:NO];
    [self.navBar.centerCityLabel setText:@"消息中心"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDataNotification:) name:@"getDataNotification" object:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.view addSubview:[self getMyLoadView]];
    [[self getHelper] doNetRequest];
}

#pragma mark - function
- (void)getDataNotification:(NSNotification *)notification
{
    NSDictionary * userData = notification.userInfo;
    [_myLoadView removeSelf];
    if (userData[@"result"]) {
        [self updateTable];
    } else {
        [RJTools showTitle:@"失败" msg:@"获取数据失败！" cancelString:@"知道了" vc:self];
    }
}

- (void)updateTable
{
    [self.view addSubview:[self getTableView]];
}

- (CGFloat)getHeightWithDetail:(NSString *)detail images:(NSArray *)images
{
    CGFloat detailWidth = ScreenWidth - 16;
    CGFloat detailHeight = [detail boundingRectWithSize:CGSizeMake(detailWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size.height + 6;

    NSUInteger imageCount = images.count;
    CGFloat imageHeight = 0;
    if (imageCount > 0) {
        imageHeight = 90 + 90 * (imageCount / 3);
    }
    return (detailHeight + imageHeight);
}

#pragma mark - getter
- (UITableView *)getTableView
{
    if (!_talbeView) {
        _talbeView = [[UITableView alloc] initWithFrame:CGRectMake(0, sysNavBarHeight, ScreenWidth, ScreenHeight - sysNavBarHeight) style:UITableViewStylePlain];
        [_talbeView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
        [_talbeView setDelegate:self];
        [_talbeView setDataSource:self];
    }
    return _talbeView;
}

- (MessageModleHelper *)getHelper
{
    if (!_helper) {
        _helper = [[MessageModleHelper alloc] init];
    }
    return _helper;
}

- (RJLoadView *)getMyLoadView
{
    if (!_myLoadView) {
        _myLoadView = [[RJLoadView alloc] initWithFrame:ScreenBounds];
    }
    return _myLoadView;
}

#pragma mark - delegete
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _helper.model.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary * dic = _helper.model[indexPath.row];
    NSString * detailString = dic[@"messageContent"];
    NSString * imageString = dic[@"messageMImgs"];
    NSArray * images = [imageString componentsSeparatedByString:@","];
    
    return 55 + [self getHeightWithDetail:detailString images:images] + 16;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell * cell = [_talbeView dequeueReusableCellWithIdentifier:@"MessageCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary * dic = _helper.model[indexPath.row];
    [cell.tittleLabel setText:dic[@"messageTitle"]];
    [cell.detailLabel setText:dic[@"messageContent"]];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:dic[@"messageLImg"]] placeholderImage:nil];
    [cell.timeLabel setText:dic[@"timePoint"]];
    [cell setDelegate:self];
    
    
    //设置高度
    CGFloat detailWidth = ScreenWidth - 16;
    CGFloat detailHeight = [dic[@"messageContent"] boundingRectWithSize:CGSizeMake(detailWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size.height + 6;
    NSString * imageString = dic[@"messageMImgs"];
    NSArray * images = [imageString componentsSeparatedByString:@","];
    NSUInteger imageCount = images.count;
    CGFloat imageHeight = 0;
    if (imageCount > 0) {
        imageHeight = 90 + 90 * (imageCount / 3);
    }
    [cell setTextHeight:detailHeight imagesHeight:imageHeight images:images];
    return cell;
}

- (void)MessageCell:(MessageCell *)cell didPressImageViewCount:(NSUInteger)count imageSize:(CGSize)size images:(NSArray *)images
{
//计算图片位于屏幕的位置
    CGRect contentRect = cell.imageContentView.frame;
    contentRect.origin.y += (cell.frame.origin.y + sysNavBarHeight - _talbeView.contentOffset.y);
    
    CGRect imageRect = contentRect;
    imageRect.origin.x += (count % 3 * 90);
    imageRect.origin.y += (count / 3 * 90);
    imageRect.size.height = 80;
    imageRect.size.width = 80;
    
    CGSize scaleSize = [RJTools reSetSize:size width:ScreenWidth];
    CGRect endImageRect = CGRectMake(0, ScreenHeight / 2 - scaleSize.height / 2, scaleSize.width, scaleSize.height);
    
//图片放大动画
    UIView * backGroundView = [[UIView alloc] initWithFrame:ScreenBounds];
    [backGroundView setBackgroundColor:[UIColor blackColor]];
    [backGroundView setAlpha:0];
    [self.view addSubview:backGroundView];
    
    UIImageView * tempImageView = [[UIImageView alloc] initWithFrame:imageRect];
    [self.view addSubview:tempImageView];
    [tempImageView sd_setImageWithURL:[NSURL URLWithString:images[count]]];
    
    [UIView animateWithDuration:0.3 animations:^{
        [backGroundView setAlpha:1];
        CGFloat y = endImageRect.origin.y;
        if (isnan(y)) {
            return ;
        }
        [tempImageView setFrame:endImageRect];
        
    } completion:^(BOOL finished) {
        if (finished) {
            MessageImageShowVC * vc = [[MessageImageShowVC alloc] initWithImages:images];
            vc.imageContentRect = contentRect;
            vc.imageCount = count;
            [self addChildViewController:vc];
            [self.view addSubview:vc.view];
            [tempImageView removeFromSuperview];
            [backGroundView removeFromSuperview];
        }
    }];
}
@end
